﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WebApiHostTest.Models;

namespace KS.DataWrapper.Test
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void CreateAndInsertTest()
        {
            Random rnd = new Random();
            try
            {
                var testCollection = new DataWrapper.DataCacheObject<TestItemClass>(new Uri("http://localhost:1193/"), null);
                testCollection.Add(new TestItemClass() { ID = "tttt" });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
               
                Assert.IsTrue(testCollection.Count > 0);

            }
            catch(Exception ex)
            {
                Assert.Fail(ex.Message);

            }
            
           
            
        }

        [TestMethod]
        public void EnumerableTest()
        {
            Random rnd = new Random();
            try
            {
                var testCollection = new DataWrapper.DataCacheObject<TestItemClass>(new Uri("http://localhost:1193/"), null);
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });

                string testID = string.Empty;
                foreach (var item in testCollection)
                {
                    item.Vts = System.DateTime.Now;
                    testID = item.ID;
                }

                var itestItem = testCollection[testID];
                testCollection[testID] = itestItem;

                Assert.IsFalse(itestItem == null);
            }
            catch (Exception ex)
            {
                Assert.Fail( ex.Message);

            }
            


        }

        [TestMethod]
        public void DeleteTest()
        {
            Random rnd = new Random();
            try
            {
                var testCollection = new DataWrapper.DataCacheObject<TestItemClass>(new Uri("http://localhost:1193/"), null);
                testCollection.Add(new TestItemClass() { ID = "abc" });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                Assert.IsTrue(testCollection.Count > 0);
                foreach (var item in testCollection)
                {
                    if(item.ID != "abc")
                    {
                        testCollection.Remove(item);
                    }
                }
                Assert.AreEqual(0, testCollection.Count);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);

            }



        }

        [TestMethod]
        public void CollectionTest()
        {
            Random rnd = new Random();
            try
            {
                var testCollection = new DataWrapper.DataCacheObject<TestItemClass>(new Uri("http://localhost:1193/"), null);
                
                var itemTEst = new TestItemClass() { ID = "qwe"};
                testCollection.Add(itemTEst);
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });
                testCollection.Add(new TestItemClass() { ID = rnd.Next().ToString() });

                Assert.AreEqual(true, testCollection.Contains(itemTEst));

                testCollection.Clear();
                Assert.AreEqual(0, testCollection.Count);

            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);

            }
        }

    }
}
